// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"fmt"
	"log"

	"os"
	"strconv"

	"github.com/lxn/walk"
	. "github.com/lxn/walk/declarative"
)

type MyMainWindow struct {
	*walk.MainWindow
	outTE        *walk.TextEdit
	prevFilePath string //路径
	Keyword      string //查找关键字
	Linefind     int    //行数
}

func main() {
	mw := new(MyMainWindow)
	var openAction *walk.Action
	var db *walk.DataBinder

	if _, err := (MainWindow{
		AssignTo: &mw.MainWindow,
		Title:    "Excel文本查找器",
		Icon:     "icon.ico",
		MenuItems: []MenuItem{
			Menu{
				Text: "&File",
				Items: []MenuItem{
					Action{
						AssignTo:    &openAction,
						Text:        "&Open",
						OnTriggered: mw.openAction_Triggered,
					},
					Separator{},
					Action{
						Text:        "Exit",
						OnTriggered: func() { mw.Close() },
					},
				},
			},

			Menu{
				Text: "&Help",
				Items: []MenuItem{
					Action{
						Text:        "关于",
						OnTriggered: mw.aboutAction_Triggered,
					},
				},
			},
		},

		MinSize: Size{320, 240},
		Size:    Size{800, 600},
		Layout:  VBox{MarginsZero: true},

		DataBinder: DataBinder{
			AssignTo:       &db,
			DataSource:     mw, //第一个问题
			ErrorPresenter: ToolTipErrorPresenter{},
		},

		Children: []Widget{
			HSpacer{},
			Label{
				Text: "待处理文件",
			},
			HSpacer{},
			TextEdit{
				AssignTo: &mw.outTE,
				ReadOnly: true,
				Text:     fmt.Sprintf("%s", mw.prevFilePath), //显示到窗口中
			},

			Label{
				Text: "查找文本",
			},
			HSpacer{},
			TextEdit{
				Text: Bind("Keyword"),
			},

			Composite{
				Layout: HBox{},
				Children: []Widget{
					PushButton{

						Text:    "打 开",
						MaxSize: Size{60, 60},
						OnClicked: func() {
							mw.openAction_Triggered()
						},
					},
					PushButton{
						Text:    "重 置",
						MaxSize: Size{60, 60},
						OnClicked: func() {
							mw.outTE.SetText("")
						},
					},
					PushButton{
						Text:    "转 换",
						MaxSize: Size{60, 60},
						OnClicked: func() {
							db.Submit() //绑定文本
							if mw.Keyword == "" || mw.prevFilePath == "" {
								walk.MsgBox(mw, "提示", "请确认打开文件并输入查找文本！", walk.MsgBoxIconInformation)
							} else {
								values := GetValues(mw) //顺便得到多少行
								if cmd, err := RunChangeDialog(mw, mw.Linefind); err != nil {
									log.Print(err)
								} else if cmd == walk.DlgCmdOK {
									if err := mw.saveExcel(values); err != nil {
										log.Print(err)
									} else {
										walk.MsgBox(mw, "提示", "保存成功！", walk.MsgBoxIconInformation)
									}

								}
							}
						},
					},
				},
			},
			HSpacer{},
		},
	}.Run()); err != nil {
		log.Fatal(err)
	}

}

func (mw *MyMainWindow) openAction_Triggered() {
	if err := mw.openExcel(); err != nil {
		log.Print(err)
	} else {
		mw.outTE.SetText(fmt.Sprintf("%+v", mw.prevFilePath))
	}
}

func (mw *MyMainWindow) openExcel() error {
	dlg := new(walk.FileDialog)

	dlg.FilePath = mw.prevFilePath
	dlg.Filter = "Excel Files (*.xlsx)|*.xlsx"
	dlg.Title = "请选择一个Excel文件"

	if ok, err := dlg.ShowOpen(mw); err != nil {
		return err
	} else if !ok {
		return nil
	}
	mw.prevFilePath = dlg.FilePath
	return nil
}

func (mw *MyMainWindow) saveExcel(myvalues map[string]string) error {
	dlg := new(walk.FileDialog)
	dlg.FilePath = mw.prevFilePath
	dlg.Filter = "Excel Files (*.xls;*.xlsx)|*.xls;*.xlsx"
	dlg.Title = "保存Excel文件"
	if ok, err := dlg.ShowSave(mw); err != nil {
		return err
	} else if !ok {
		return nil
	}
	err1 := SaveDatas(myvalues, dlg.FilePath)
	if err1 != nil {
		fmt.Println(err1)
		os.Exit(1)
	}
	return nil
}

func (mw *MyMainWindow) aboutAction_Triggered() {
	walk.MsgBox(mw, "关于", "嘿man,一起say yoyo", walk.MsgBoxIconInformation)
}

//显示查找到多少行的对话框
func RunChangeDialog(owner walk.Form, lineNum int) (int, error) {
	var dlg *walk.Dialog //对话框
	var acceptPB, cancelPB *walk.PushButton
	linecount := strconv.Itoa(lineNum)

	return Dialog{
		AssignTo:      &dlg,
		Title:         "查找结果",
		DefaultButton: &acceptPB,
		CancelButton:  &cancelPB,
		MinSize:       Size{300, 300},
		Layout:        VBox{},
		Children: []Widget{
			Composite{
				Layout: HBox{},
				Children: []Widget{
					Label{
						Text: "找到" + linecount + "行,是否保存？",
					},
				},
			},

			Composite{
				Layout: HBox{},
				Children: []Widget{
					HSpacer{},
					PushButton{
						AssignTo: &acceptPB,
						Text:     "保存",
						OnClicked: func() { //这个来保存数据
							dlg.Accept()
						},
					},
					PushButton{
						AssignTo:  &cancelPB,
						Text:      "取消",
						OnClicked: func() { dlg.Cancel() },
					},
				},
			},
		},
	}.Run(owner)
}
